<?php

class Core_Model_DbTable_GeneralInformation extends Zend_Db_Table_Abstract {

    protected $_name = 'general_information';

    const prefix_ref = "EPAC";

    public function generateRefNo() {
        $last_id = $this->_getTrackingRow();
        if ($last_id < 10000) {
            $float_suffix = strval((float) ($last_id / 10000));
            $suffix_ref = substr($float_suffix, 2);
            if ( strlen($suffix_ref) < 4 ) {
                $suffix_ref .= '0';
            }
        } else {
            $suffix_ref = $last_id;
        }
        return self::prefix_ref . '-' . date('Y') . '-' . $suffix_ref;
    }

    private function _getTrackingRow() {
        $session = Zend_Registry::get("session");
        $user_id = $session->user->id;

        $tracking_db = new Core_Model_DbTable_TrackingInput();
        return $tracking_db->getTrackingRow($user_id);
    }
    
    public function getPaginatorAdapter($filters) {
        $select = $this->select()
                       ->setIntegrityCheck(false)
                       ->from("$this->_name as gi")
                       ->joinLeft('ports AS p', 'gi.port_of_call = p.id', array ('port_name' => 'name'))
                       ->joinLeft('users as user', 'gi.customer = user.id', array('fullname'));
        if ( !empty($filters['ref_no']) ) {
            $cond = $this->getAdapter()->quoteInto("ref_no = ?", $filters['ref_no']);
            $select->where($cond);
        }
        if ( !empty($filters['vessel']) ) {
            $cond = $this->getAdapter()->quoteInto("vessel LIKE ?", "%" . $filters['vessel'] . "%");
            $select->where($cond);
        }
        if ( !empty($filters['port_of_call']) ) {
            $cond = $this->getAdapter()->quoteInto("port_of_call = ?", $filters['port_of_call']);
            $select->where($cond);
        }
        if ( !empty($filters['ata']) ) {
            $date = date("Y-m-d", $filters['ata']);
            $cond = $this->getAdapter()->quoteInto("FROM_UNIXTIME(ata, '%Y-%m-%d') = ?", $date);
            $select->where($cond);
        }
        if ( !empty($filters['ats']) ) {
            $date = date("Y-m-d", $filters['ats']);
            $cond = $this->getAdapter()->quoteInto("FROM_UNIXTIME(ats, '%Y-%m-%d') = ?", $date);
            $select->where($cond);
        }
        
        $select->order("updated_time DESC");

        $this->injectPermission($select);
        if($select == null){
            return null;
        }
        return new Zend_Paginator_Adapter_DbSelect($select);
    }

    private function injectPermission(&$select){
        $session = Zend_Registry::get("session");
        $user = $session->user;
        if($user){
            switch(strtolower($user->role)){
                case strtolower(Core_Role::ROLE_PRINCIPAL):
                    $select->where($this->getAdapter()->quoteInto("customer = ?", $user->id));
                    break;
                case strtolower(Core_Role::ROLE_INPUT):
                case strtolower(Core_Role::ROLE_ADMIN):
                    break;
                case strtolower(Core_Role::ROLE_GUEST):
                default:
                    $select = null;
                    break;
            }
        }
    }
    
    public function getById($id) {
        $cond = $this->getAdapter()->quoteInto("id = ?", $id);
        return $this->fetchRow($cond);
    }
    
    public function setUsedTrackingInput($ref_no) {
        $splits = explode("-", $ref_no);
        $tracking_number = intval(end($splits));
        $track_db = new Core_Model_DbTable_TrackingInput();
        $track_db->setUsedTrackingInput($tracking_number);
    }
    

}

?>
